﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using COMExcel = Microsoft.Office.Interop.Excel;

namespace BSPortal
{
    public class ExcelManage
    {
        public static void Export(List<short> orderList)
        {
            int line = 1;

            var mainService = Common.GetClientService();
            // Khởi động chtr Excell
            COMExcel.Application exApp = new COMExcel.Application();

            // Thêm file temp xls
            COMExcel.Workbook exBook = exApp.Workbooks.Add(COMExcel.XlWBATemplate.xlWBATWorksheet );

            // Lấy sheet 1.
            COMExcel.Worksheet exSheet = (COMExcel.Worksheet)exBook.Worksheets[1];
            exSheet.Activate();
            exSheet.Name = "Order List";

            // Hiển thị chương trình excel
            exApp.Visible = true;

            // Write title
            COMExcel.Range rTitle = (COMExcel.Range) exSheet.Cells[line, 7];
            rTitle.Value2 = "Danh sách đơn đặt hàng";
            rTitle.Columns.AutoFit();

            line += 3;

            //Write report info
            var user = mainService.GetUserById(System.Web.HttpContext.Current.Session["Username"].ToString());
            ExcellWriter(exSheet, line, 1, "Người lập:");
            ExcellWriter(exSheet, line, 2, user.Name);
            line += 1;
            ExcellWriter(exSheet, line, 1, "Ngày lập:");
            ExcellWriter(exSheet, line, 2, DateTime.Today.ToString("dd/MM/yyyy hh:mm"));
            line += 3;

            foreach (short orderId in orderList)
            {
                //Write customer info
                WriteCustomerInfo(exSheet, ref  line, orderId);

                //Write customer's product info
                #region Write product title
                ExcellWriter(exSheet, line, 3, "Id");
                ExcellWriter(exSheet, line, 4, "Tên sản phẩm");
                ExcellWriter(exSheet, line, 5, "Giá");
                ExcellWriter(exSheet, line, 6, "Số lượng");
                ExcellWriter(exSheet, line, 7, "Nhà cung cấp");
                line += 1;
                #endregion

                WriteOrderDetail(exSheet, ref  line, orderId);
            }

        }

        static void ExcellWriter(COMExcel.Worksheet exSheet, int row, int column, string value)
        {
            COMExcel.Range r = (COMExcel.Range)exSheet.Cells[row, column];
            r.Value2 = value!=null?value:"";
            if ((double)r.Columns.ColumnWidth < (value != null ? value.Length : 0))
                r.Columns.AutoFit();
        }

        static void WriteCustomerInfo(COMExcel.Worksheet exSheet, ref int line, short orderId)
        {
            #region
            var mainService = Common.GetClientService();
            var order = mainService.GetOrderById(orderId);

            ExcellWriter(exSheet, line, 1, "Khách hàng:");
            ExcellWriter(exSheet, line, 2, order.Name);
            line += 1;
            ExcellWriter(exSheet, line, 1, "Địa chỉ:");
            ExcellWriter(exSheet, line, 2, order.Address);
            line += 1;
            ExcellWriter(exSheet, line, 1, "Điện thoại:");
            ExcellWriter(exSheet, line, 2, "'" + order.Phone);
            line += 1;
            ExcellWriter(exSheet, line, 1, "Phí:");
            ExcellWriter(exSheet, line, 2, "'" + order.Fee);
            line += 1;
            ExcellWriter(exSheet, line, 1, "Thanh toán:");
            ExcellWriter(exSheet, line, 2, Common.GetOrderPayString((int)order.PayType));
            line += 1;
            ExcellWriter(exSheet, line, 1, "Trạng thái:");
            ExcellWriter(exSheet, line, 2, Common.GetOrderStatusString((int)order.Status));
            line += 1;
            #endregion
        }

        static void WriteOrderDetail(COMExcel.Worksheet exSheet, ref int line, short orderlId)
        {
            #region
            var mainService = Common.GetClientService();
            var listOrderDetail = mainService.GetListOrderDetailByOrderId(orderlId);
            double total = 0;
            foreach (var orderDetail in listOrderDetail)
            {
                var product = mainService.GetFullProductById(orderDetail.ProductId);
                ExcellWriter(exSheet, line, 3, "'" + product.ProductId);
                ExcellWriter(exSheet, line, 4, product.Name);
                ExcellWriter(exSheet, line, 5, "'" + Math.Round(product.Price*(1-(double)product.Discount)));
                ExcellWriter(exSheet, line, 6, "'" + orderDetail.Amount);
                ExcellWriter(exSheet, line, 7, product.Distributor);
                line += 1;
                total += Math.Round(product.Price * (1 - (double)product.Discount));
            }

            ExcellWriter(exSheet, line, 3, "Tổng");
            ExcellWriter(exSheet, line, 5, "'" + total);
            line += 2;
            #endregion
        }
    }
}